package com.jiantong.work.impl.service.yangzhibaoxian;

/**
 * Created by ThinkPad on 2017/9/5.
 */

import com.google.common.base.Throwables;

import com.jiantong.work.impl.dao.yangzhibaoxian.FileDao;
import com.jiantong.work.model.yangzhibaoxian.File;
import com.jiantong.work.service.yangzhibaoxian.FileService;
import io.terminus.boot.rpc.common.annotation.RpcProvider;
import io.terminus.common.model.PageInfo;
import io.terminus.common.model.Paging;
import io.terminus.common.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;

/**
 * 文件表
 */
@Slf4j
@Service
@RpcProvider
public class FileServiceImpl implements FileService {
    private FileDao fileDao;

    @Autowired
    public FileServiceImpl(FileDao fileDao) {
        this.fileDao = fileDao;
    }


    @Override
    public Response<File> findById(Long id) {
        try {
            File file = (File) fileDao.findById(id);
            if (file == null) {
                log.error("file(id={}) is not found", id);
                return Response.fail("file.not.found");
            }

            return Response.ok(file);
        } catch (Exception e) {
            log.error("failed to find file(id={}), cause:{}", id, Throwables.getStackTraceAsString(e));
            return Response.fail("file.find.fail");
        }
    }

    @Override
    public Response<List<File>> findByIds(List ids) {
        try {
            List<File> fileList = fileDao.findByIds(ids);
            if (fileList == null) {
                log.error("fileList(ids={}) is not found", ids);
                return Response.fail("fileList.not.found");
            }

            return Response.ok(fileList);
        } catch (Exception e) {
            log.error("failed to find fileList(ids={}), cause:{}", ids, Throwables.getStackTraceAsString(e));
            return Response.fail("fileList.find.fail");
        }

    }

    @Override
    public Response<List<File>> list(Map map) {
        try {
            List<File> fileList = fileDao.list(map);
            if (fileList == null) {
                log.error("fileList(ids={}) is not found", map);
                return Response.fail("fileList.not.found");
            }

            return Response.ok(fileList);
        } catch (Exception e) {
            log.error("failed to find fileList(ids={}), cause:{}", map, Throwables.getStackTraceAsString(e));
            return Response.fail("fileList.find.fail");
        }
    }

    @Override
    public Response<Paging<File>> ping(Integer offset, Integer limit, Map map) {
        try {
            PageInfo pageInfo=new PageInfo(offset,limit);
            Paging<File> filePaging = fileDao.paging(pageInfo.getOffset(), pageInfo.getLimit(), map);
            if (filePaging == null) {
                log.error("filePaging(ids={}) is not found", map);
                return Response.fail("filePaging.not.found");
            }

            return Response.ok(filePaging);
        } catch (Exception e) {
            log.error("failed to find filePaging(ids={}), cause:{}", map, Throwables.getStackTraceAsString(e));
            return Response.fail("filePaging.find.fail");
        }
    }

    @Override
    public Boolean update(File file) {
        try {
            Boolean b = fileDao.update(file);
            return b;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    public Long create(File file) {
        try {
            fileDao.create(file);
            return file.getId();
        } catch (Exception e) {
            e.printStackTrace();
            return -500L;
        }
    }

    @Override
    public Integer creates(List<File> fileList) {
        try {

            return fileDao.creates(fileList);
        } catch (Exception e) {
            e.printStackTrace();
            return -500;
        }
    }

}
